#include <iostream>
#include <string>
#include <stdio.h>

#include "data.h"

using namespace std;

Data::Data(string fileName){
	this->fileName = fileName;
	this->init();
}

Data::~Data(){

}

void Data::init() {
	char c;
	string line;

	ifstream dataFile(this->fileName.c_str());

	int nRows = 0;
	int nCols = 0;
	while(getline(dataFile, line)) {
		int n = 0;
		vector<char> row;
		const int length = line.length();

		for(int i = 0; i < length; i++){
			if (line[i] != ' '){
				row.push_back(line[i]);
				n ++;
			}
		}

		if(nRows == 0) 
			nCols = n;

		if (n == nCols){ // If the number of items in this row are the same 
                                 // as the number in the first row. To avoid empty 
                                 // newlines at the end of the datafile
			nRows ++;
			this->data.push_back(row);
		}
	}

	this->nInds = nRows;
	this->nSteps = nCols;

	dataFile.close();
}

void Data::setDimensions(int &nInds, int &nSteps) {
	nInds  = this->nInds;
	nSteps = this->nSteps;
}

vector<charVec> Data::getData(){
	return data;
}

int Data::getInt(char c) {
	return atoi(&c);
}

vector<int> Data::getGrpCountVector() {
	vector<int> grpCountVector;

	for (int j = 0; j < nSteps; j++) {
		vector<int> tempVec;

		for (int i = 0; i < nInds; i++) {
			char c = data[i][j];
			vector<int>::iterator i = find(tempVec.begin(),
							tempVec.end(), c);

			if (i == tempVec.end())
				tempVec.push_back(c);
		}
		grpCountVector.push_back(tempVec.size());
	}

	return grpCountVector;
}

int Data::getGrpCount(vector<int> grpCountVector) {
	vector<int>::iterator itr = grpCountVector.begin();

	int grpCount = 0;
	while(itr != grpCountVector.end()) {
		grpCount += *itr++;
	}

	return grpCount;
}
